// cm_onlineclass/pages/list/list.js
const app = getApp();
const WxNotificationCenter = require('../../../common/utils/WxNotificationCenter/WxNotificationCenter');
WxNotificationCenter.postNotificationName(app.INJECT_BLOBAL_EVENT, {
  global: require('../../global'),
});
const { config } = require('../../../common/utils/ws/ws-config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    roomList: [],
    listInterval: null,
    isSetting: false, // 验证配置
    isServer: false, // 验证服务
    floatItems: [{
      type: 0, // 自定义事件
      show: true,
      iconClass: 'icon icon-rl',
      event: 'myEvent1',
    }],
    showLogin: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 测试直播配置
    this.checkImSetting();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let { isSetting, isServer, listInterval } = this.data;
    if (isSetting && isServer) {
      clearInterval(listInterval);
      this.data.listInterval = setInterval(() => {
        this.getRoomList();
      }, 5000);
    }
  },

  // 测试配置
  checkImSetting() {
    app.util.request({
      url: 'entry/wxapp/index',
      data: {
        op: 'checkImSetting',
        uniacid: `${config.uniacid}`,
      },
      module: 'cm_trtc',
      success: res => {
        let { errno, data: { msg: errMsg }} = res.data;
        if (errno) {
          wx.showModal({
            title: '提示',
            content: errMsg,
            showCancel: false,
            success: res => {
              wx.navigateBack({});
            },
          });
          return;
        } else {
          this.data.isSetting = true;
          // 测试直播服务
          this.checkNode();
        }
      },
    });
  },
  // 测试直播服务是否开启
  checkNode() {
    app.util.request({
      url: 'entry/wxapp/index',
      data: {
        op: 'checkNode',
        uniacid: `${config.uniacid}`,
      },
      module: 'cm_trtc',
      success: res => {
        let { errno, data: { msg: errMsg }} = res.data;
        if (errno) {
          wx.showModal({
            title: '提示',
            content: errMsg,
            showCancel: false,
            success: res => {
              wx.navigateBack({});
            },
          });
          return;
        } else {
          this.data.isServer = true;
          this.onShow();
          this.getRoomList();
        }
      },
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    let {listInterval} = this.data;
    if (listInterval) {
      clearInterval(listInterval);
      this.data.listInterval = null;
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let {listInterval} = this.data;
    if (listInterval) {
      clearInterval(listInterval);
      this.data.listInterval = null;
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getRoomList();
    wx.stopPullDownRefresh();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  getRoomList() {
    app.util.request({
      url: 'entry/wxapp/tic',
      data: {
        op: 'get_room_list',
      },
      module: 'cm_onlineclass',
      success: res => {
        if (res.data && res.data.code === 0) {
          this.setData({
            roomList: res.data.rooms,
          });
        }
      },
    });
  },

  goRoom(e) {
    let that = this;
    const data = e.currentTarget.dataset;
    const roomID = data.roomid;
    const isPublic = data.needpwd == '1' ? '0' : '1';
    let roomList = this.data.roomList;
    let curobj=  {};
    for(let i = 0, lg = roomList.length; i < lg; i++) {
      if(String(roomList[i].roomID) === String(roomID)) {
        curobj = roomList[i];
        break;
      }
    }
    if(Number(isPublic) === 0) {
      this.setData({ showLogin: true});
      this.data.curobj = curobj;
    } else {
      app.videoVisiterRecord(this.data.roomID, 'cm_onlineclass'); // 音视频参与人员记录接口
      setTimeout(function(){
        that.setData({ showLogin: false })
      }, 500)
      app.util.navigateTo('cm_onlineclass/pages/room/room',{
        ...curobj,
        isPublic
      },false);
    }
  },

  enter() {
    let nowTime = new Date();
    if (nowTime - this.data.tapTime < 1000) {
      return;
    }
    this.getRoomInfo();
    this.data.tapTime = nowTime;
  },

  
  getRoomInfo() {
    let that = this;
    wx.showLoading({
      title: '加载中...',
      mask: true,
    });
    wx.showNavigationBarLoading();
    let curobj = this.data.curobj;
    app.util.request({
      url: 'entry/wxapp/tic',
      data: {
        op: 'getroom',
        roomid: curobj.roomID,
        roomSign: this.data.roomPassword || '',
      },
      module: 'cm_onlineclass',
      success: res => {
        if (res.data && res.data.errno === 0 && res.data.message) {
          app.util.navigateTo('cm_onlineclass/pages/room/room',{
            ...curobj
          },false)
          setTimeout(function(){
            that.setData({ showLogin: false })
          }, 500)
          wx.hideNavigationBarLoading();
        }
      },
      fail: err => {
        wx.showModal({
          title: '系统提示',
          content: err.data && err.data.message,
          showCancel: false,
          cancelText: '取消',
        });
        wx.hideNavigationBarLoading()
      },
    });
  },

  hideLogin(){
    this.setData({
      showLogin: !this.data.showLogin  
    })
  },

  toLiveHistory() {
    app.util.navigateTo('cm_onlineclass/pages/live-history/live-hisrory');
  },

  getPassword(e) {
    this.data.roomPassword = e.detail;
  },
});
